import 'package:flutter/material.dart';
import 'package:flutter_ability/custom_ui.dart';

/// list-item自定义控件
class YJListTile extends StatelessWidget {
  const YJListTile({
    Key? key,
    this.onTap,
    this.onLongPress,
    this.bgColor,
    this.padding = const EdgeInsets.symmetric(horizontal: 16),
    this.margin = const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
    this.titleText,
    this.subTitleText,
    this.hintTitleText,
    this.title,
    this.iconTitle,
    this.subTitle,
    this.hintTitle,
    this.avatar,
    this.iconRight,
    this.listItemTextColor = YJColors.BLACK85,
    this.enabled = true,
    this.selected = false,
    this.focusColor,
    this.hoverColor,
    this.focusNode,
    this.autofocus = false,
    this.radius,
    this.shadow,
  }) : super(key: key);

  /// 正文本
  final String? titleText;

  /// 副文本
  final String? subTitleText;

  /// 提示内容
  final String? hintTitleText;

  /// 背景颜色
  final Color? bgColor;

  /// 头像
  final Widget? avatar;

  /// 正文本Widget
  final Widget? title;

  /// 正文本Icon Widget
  final Widget? iconTitle;

  /// 副文本Widget
  final Widget? subTitle;

  /// 右边icon">"
  final Widget? iconRight;

  /// 提示内容Widget
  final Widget? hintTitle;

  /// 外边距
  final EdgeInsets margin;

  /// 内边距
  final EdgeInsets padding;

  /// 是否激活状态
  final bool enabled;

  /// 按钮点击事件
  final GestureTapCallback? onTap;

  /// 按钮长按事件
  final GestureLongPressCallback? onLongPress;

  /// 选中
  final bool selected;

  /// 获取焦点颜色
  final Color? focusColor;

  /// text颜色
  final Color? listItemTextColor;

  /// 悬乎颜色
  final Color? hoverColor;

  /// 焦点
  final FocusNode? focusNode;

  /// 自动获焦
  final bool autofocus;

  /// 圆角
  final double? radius;

  /// 阴影
  final BoxShadow? shadow;

  @override
  Widget build(BuildContext context) => InkWell(
        onTap: enabled ? onTap : null,
        onLongPress: enabled ? onLongPress : null,
        canRequestFocus: enabled,
        focusNode: focusNode,
        focusColor: focusColor,
        hoverColor: hoverColor,
        autofocus: autofocus,
        child: Semantics(
          selected: selected,
          enabled: enabled,
          child: Container(
            padding: padding,
            margin: margin,
            decoration: BoxDecoration(
              color: bgColor,
              borderRadius: BorderRadius.all(Radius.circular(radius ?? 0)),
              boxShadow: buildBoxShadow(),
            ),
            child: buildRow(),
          ),
        ),
      );

  Widget buildRow() {
    return Row(
      children: [
        /// 头像
        avatar != null
            ? Container(
                width: YJSize.dp(40),
                height: YJSize.dp(40),
                child: avatar,
                margin: EdgeInsets.only(right: YJSize.dp(16)),
              )
            : SizedBox(),
        Expanded(child: buildLiftView()),
        buildRightView()
      ],
    );
  }

  /// 左侧正副标题信息
  Widget buildLiftView() {
    return Container(
      margin: EdgeInsets.symmetric(vertical: YJSize.dp(16)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          iconTitle != null
              ? Container(
                  child: iconTitle,
                  height: YJSize.dp(24),
                  color: Colors.red,
                  margin: EdgeInsets.only(right: YJSize.dp(8)),
                )
              : SizedBox(),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [titleTextView(), subTitleTextView()],
          ),
        ],
      ),
    );
  }

  /// 副文本
  Widget subTitleTextView() {
    return subTitleText != null
        ? Container(
            height: YJSize.dp(24),
            child: TextView.text(subTitleText,
                fontSize: YJSize.SP_14, color: YJColors.BLACK45),
          )
        : subTitle != null
            ? Container(
                child: subTitle,
                height: YJSize.dp(24),
              )
            : SizedBox();
  }

  /// 正文本
  Widget titleTextView() {
    return titleText != null
        ? Container(
            height: YJSize.dp(24),
            child: TextView.text(titleText,
                maxLines: 1,
                overflows: TextOverflow.ellipsis,
                fontSize: YJSize.SP_16,
                color: listItemTextColor),
          )
        : title != null
            ? Container(
                height: YJSize.dp(24),
                child: title,
              )
            : SizedBox();
  }

  /// 右测文字icon信息
  Widget buildRightView() {
    return Row(
      children: [
        hintTitleText != null
            ? Container(
                child: TextView.text(hintTitleText,
                    fontSize: YJSize.SP_14, color: YJColors.BLACK45),
              )
            : hintTitle != null
                ? Container(
                    child: hintTitle,
                  )
                : SizedBox(),
        iconRight != null
            ? Container(
                margin: EdgeInsets.only(left: YJSize.dp(8)),
                child: iconRight,
              )
            : Container(
                margin: EdgeInsets.only(left: YJSize.dp(8)),
                child: Icon(
                  Icons.arrow_forward_ios,
                  size: YJSize.SP_16,
                  color: Colors.black87,
                ),
              ),
      ],
    );
  }

  buildBoxShadow() {
    BoxShadow box = BoxShadow(
      color: Colors.grey.withOpacity(0.5),
      spreadRadius: 4,
      blurRadius: 7,
      offset: const Offset(2, 4), // changes position of shadow
    );
    return shadow;
  }
}
